package com.wd.base.util;

import android.text.TextUtils;
import android.util.Log;

/**
 * @author W4255
 * @version [V1.0.0.000, 2021/1/24 21:34]
 * @Description: NA
 */
public class Logger {
    public static boolean DEBUG = true;
    private static Logger mLogger;
    private static final String USER_NAME = "Logger";

    private Logger() {
    }

    /**
     * Get The Current Function Name
     *
     * @return Name
     */
    private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts == null) {
            return null;
        }
        for (StackTraceElement st : sts) {
            if (st.isNativeMethod()) {
                continue;
            }
            if (st.getClassName().equals(Thread.class.getName())) {
                continue;
            }
            if (st.getClassName().equals(this.getClass().getName())) {
                continue;
            }
//            return USER_NAME + "[" + Thread.currentThread().getName() + ": "
//                    + st.getFileName() + ":" + st.getLineNumber() + " "
//                    + st.getMethodName() + "]";
            return USER_NAME + "[" + Thread.currentThread().getName() + ":"
                    + st.getFileName() + ":" + st.getLineNumber() + "]";
        }
        return null;
    }

    public static void i(Object str) {
        print(Log.INFO, null, str);
    }

    public static void i(String tag, Object str) {
        print(Log.INFO, tag, str);
    }

    public static void d(Object str) {
        print(Log.DEBUG, null, str);
    }

    public static void d(String tag, Object str) {
        print(Log.DEBUG, tag, str);
    }

    public static void v(Object str) {
        print(Log.VERBOSE, null, str);
    }

    public static void w(Object str) {
        print(Log.WARN, null, str);
    }

    public static void w(String tag, Object str) {
        print(Log.WARN, tag, str);
    }

    public static void e(Object str) {
        print(Log.ERROR, null, str);
    }

    public static void e(String tag, Object str) {
        print(Log.ERROR, tag, str);
    }

    /**
     * 用于区分不同接口数据 打印传入参数
     *
     * @param index
     * @param tag
     * @param str
     */

    private static void print(int index, String tag, Object str) {
        String tagName = tag;
        if (TextUtils.isEmpty(tag)) {
            if (mLogger == null) {
                mLogger = new Logger();
            }
            tagName = mLogger.getFunctionName();
        }
        if (!DEBUG) {
            if (index <= Log.DEBUG) {
                return;
            }
        }
        switch (index) {
            case Log.VERBOSE:
                Log.v(tagName, str.toString());
                break;
            case Log.DEBUG:
                Log.d(tagName, str.toString());
                break;
            case Log.INFO:
                Log.i(tagName, str.toString());
                break;
            case Log.WARN:
                Log.w(tagName, str.toString());
                break;
            case Log.ERROR:
                Log.e(tagName, str.toString());
                break;
            default:
                break;
        }
    }

}
